tongfusoft.roleHasPrivileges = function() {
}

tongfusoft.roleHasPrivileges.prototype = {
	// 角色列表
	roleTable : function() {

		var table = new tongfusoft.table();
		var columns = [
				{
					"data" : "roleId"
				},
				{
					"data" : "roleName"
				},
				{
					"data" : "loginName"
				},
				{
					"data" : "lastUpdDate"
				} ]

		table.dataTable('roleTable', 'manager/role/query', columns, null);

		$('#roleTable').DataTable().on(
				'select',
				function(e, dt, type, indexes) {
					$("#roleId")
							.val($('#roleTable').DataTable().rows(indexes).data()[0].roleId);
					
					$("#roleTableTitle").text("：" + $('#roleTable').DataTable().rows(indexes).data()[0].roleName);

					$('#privilegeTable').DataTable().clear();
					$('#privilegeTable').DataTable().ajax.url(
							common.getContextPath('manager/role/privilegesByRole?roleId='
									+ $("#roleId").val())).load();
				});

	},
	
	privilegeTable : function() {
		var table = new tongfusoft.table();
		var columns = [
			{
				"data" : "rpId"
			},
			{
				"data" : "priName"
			},
			{
				"data" : "priDesc"
			},
			{
				"data" : "rpId",
				"render" : function(data, type, full, meta) {
					var delbut = '<button rpId="'
							+ data
							+ '" onclick="new tongfusoft.roleHasPrivileges().delPrivilege(this)" type="button" data-toggle="modal" data-target="#delrole" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
					return delbut;
				}
			}
		]
		
		table.dataTable('privilegeTable', 'manager/role/privilegesByRole?roleId=' + $("#roleId").val(), columns, null);
		
		/*$('#privilegeTable').DataTable().on(
				'select',
				function(e, dt, type, indexes) {
					$("#rpId").val($('#privilegeTable').DataTable().rows(indexes).data()[0].rpId);
				});*/
	},
	
	delPrivilege: function(obj){
		if(obj != null){
			swal({
				title : "是否确认?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"rpId" : $(obj).attr("rpId")
				}, 'manager/role/delPrivilege', function(data) {
					if (data.state == 'success') {
						$('#roleTable').DataTable().ajax.reload();
						$('#privilegeTable').DataTable().ajax.reload();
					}else{
						toastr["error"]('删除失败！');
					}

				}, "del")
			});
		}
	},
	
	addPrivilege: function(){
		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("priForm");
				initData['roleId'] = $("#roleId").val();
				var privileges = "";
				var pris = document.getElementsByName("choicePri");
				$("[name='choicePri']").each(function(){
					if($(this).is(':checked')){
						privileges += $(this).val() + "," ;
					}
				});
				initData['privileges'] = privileges;
				var url = 'manager/role/addPrivileges';

				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {
						$('#myModal').modal('hide');
						$('#privilegeTable').DataTable().ajax.reload();
						toastr["success"]('保存成功!');
					} else {
						toastr["error"]('添加权限失败!');
					}
				})
			}
		});
		
		$("#priForm").validate();

		$("#addPriBtn").click(function() {
			$("#priForm").submit();
		})
	},
		
	altPrivilegeTable : function() {
		$("#addAtlPrivileges").click(function() {
			var roleId = $("#roleId").val();
			if(roleId ==null || roleId ==""){
				toastr["error"]('请先选择角色!');
			}else{
				common.post('manager/role/altPrivilegesByRole', {
				"roleId" : roleId,
				
			}, function(data) {
				
				$("#priData").html('');
				var priData='';
				if (data.state == 'success') {
					$.each(data.data, function(index, item) {
						var priHtml =        ' <li style="clear:both;list-style-type: none">';
						priHtml = priHtml + ' <div class="col-md-1" style="width:50px;">';
						priHtml = priHtml +       '<div class="checkbox c-checkbox">';	
						priHtml = priHtml +        '<label><input type="checkbox" name="choicePri" value="'+item.priId+'"/> <span class="fa fa-check"></span></label>'
						priHtml = priHtml +       '</div>';	
						priHtml = priHtml + '</div>';		
						priHtml = priHtml + '<div class="col-md-6">';	
						priHtml = priHtml +    '<div style="padding-top:7px;">'+item.priName +'</div>';	
						priHtml = priHtml + '</div>';	
						priHtml = priHtml + '</li>';	
						
						priData =priData+priHtml;
					});
				}
				$("#priData").html(priData);
			});
				
				$('#myModal').modal('show');
			}

		})

	},
	
	//全选
	allCheck : function(){
		var pris = document.getElementsByName("choicePri");
		$("#choicePriAll").click(function() {
			if(choicePriAll.checked == true) {
				for(var i = 0; i < pris.length; i++){
					pris[i].checked = true;
				}
			 } else {
				 for(var i = 0; i < pris.length; i++){
					 pris[i].checked = false;
					}
			 }
		});

	}
	
	

}

$(function() {
	var rp = new tongfusoft.roleHasPrivileges();
	// 角色表表格
	rp.roleTable();
	rp.privilegeTable();
	rp.delPrivilege();
	rp.altPrivilegeTable();
	rp.allCheck();
	rp.addPrivilege();
	
})